﻿<?php
class mysql {

	var $linkID;
	var $dbHost;
	var $dbUser;
	var $dbPwd;
	var $dbName;
	var $dbPrefix;
	var $result;
	var $queryString;
	var $parameters;
	var $charset;
	var $comparison   = array('eq'=>'=','neq'=>'!=','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE');
    var $selectSql  = 'SELECT%DISTINCT% %FIELDS% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT%';
	protected $dbType           = null;
	//用外部定义的变量初始类，并连接数据库
	function __construct($pconnect = false, $nconnect = true) {
		$this->close = false;
		if ($nconnect) {
			$this->Init($pconnect);
		}
	}

	//初始化数据
	function Init($pconnect = false) {
		$this->linkID = 0;
		$this->queryString = '';
		$this->parameters = Array ();
		$this->dbHost = $GLOBALS['cfg']['db_host'];
		$this->dbUser = $GLOBALS['cfg']['db_user'];
		$this->dbPwd = $GLOBALS['cfg']['db_pass'];
		$this->dbName = $GLOBALS['cfg']['db_name'];
		$this->dbPrefix = $GLOBALS['cfg']['tb_pre'];
		$this->charset = $GLOBALS['cfg']['db_charset'];
		$this->result["me"] = 0;
		$this->connect($GLOBALS['pconnect']);
		
	
	}

	//连接数据库
	function connect($pconnect = false) {
		//连接数据库
			if (!$pconnect) {
				$this->linkID = @ mysql_connect($this->dbHost, $this->dbUser, $this->dbPwd);
			} else {
				$this->linkID = @ mysql_pconnect($this->dbHost, $this->dbUser, $this->dbPwd);
			}

		//处理错误，成功连接则选择数据库
		if (!$this->linkID) {
			$this->mysql_errormsg('数据库连接失败,请查看数据库服务是否启动!');
			exit ();
		}
		@ mysql_select_db($this->dbName);
		$mysqlver = explode('.', $this->version());
		$mysqlver = $mysqlver[0] . '.' . $mysqlver[1];
		if ($mysqlver > 4.0) {
			@ mysql_query("SET NAMES '" . $GLOBALS['cfg_db_language'] . "', character_set_client=binary, sql_mode='' ;", $this->linkID);
		}

		if (!@ mysql_query("SET NAMES $this->charset")) {
			$this->mysql_errormsg('设置字符错误!');
		}
		return true;
	}

	function select_db($dbname) {
		return mysql_select_db($dbname);
	}

	//设置SQL里的参数
	function SetParameter($key, $value) {
		$this->parameters[$key] = $value;
	}

	//获得MySql的版本号
	function version($isformat = true) {
		global $db;
		if ($this->close) {
			$this->connect(false);
			$this->close = false;
		}
		$rs = mysql_query("SELECT VERSION();", $this->linkID);
		$row = mysql_fetch_array($rs);
		$mysql_version = $row[0];
		mysql_free_result($rs);
		if ($isformat) {
			$mysql_versions = explode(".", trim($mysql_version));
			$mysql_version = number_format($mysql_versions[0] . "." . $mysql_versions[1], 2);
		}
		return $mysql_version;
	}

	function query($sql, $type = '') {
		$query = mysql_query($sql,$this->linkID);
		if (!$query && $type != 'SILENT') {
			$this->mysql_errormsg('SQL语句语法错误!');
		}
		//$this->querycount++;
		return $query;
	}

	function affected_rows($sql) {
		$this->query($sql);

		return mysql_affected_rows();
	}

	/**
	 * 取得记录集,获取数组-索引和关联
	 *
	 * @access public
	 * @return void
	 */

	function fetch_array($query,$result_type = MYSQL_ASSOC) {
	

		return @mysql_fetch_array($query, $result_type);
	}

	/**
	 * 获取数字索引数组,使用$row[0],$row[1],$row[2]
	 *
	 * @access public
	 * @return void
	 */
	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}

	function fetch_one_array($query) {
		$result = $this->query($query);
		$record = $this->fetch_array($result);
		return $record;
	}

	function fetch_one($query) {
		$record = $this->fetch_one_array($query);
		Return $record[0];
	}

	/**
	 * 计算结果集条数
	 *
	 * @access public
	 * @return integer
	 */
	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	function fetch_all($query) {
		$arr = array ();
		//$query = $this->query($sql);
		while ($data = $this->fetch_array($query)) {
			$arr[] = $data;
		}
		return $arr;
	}

	/**
	 * 取得上一步 INSERT 操作产生的 ID
	 *
	 * @access public
	 * @return integer
	 */
	public function insert_id() {
		return mysql_insert_id();
	}

	//释放记录
	function free_result($query) {
		$query = mysql_free_result($query);
		return $query;
	}

	//关闭连接
	function close() {
		return mysql_close();
	}

	function error() {
		return @ mysql_error();
		//从先前MySQL操作传回错误讯息
	}

	function errno() {
		return @ intval(mysql_errno());
		//从先前MySQL操作传回错误讯息代号
	}
	/**
	     +----------------------------------------------------------
	     * set分析
	     +----------------------------------------------------------
	     * @access protected
	     +----------------------------------------------------------
	     * @param array $data
	     +----------------------------------------------------------
	     * @return string
	     +----------------------------------------------------------
	     */
	protected function parseSet($data) {
		$setStr = '';
		foreach ($data as $key => $val) {
			$value = $this->parseValue($val);
			if (is_scalar($value)) { // 过滤非标量数据
				$set[] = $this->addSpecialChar($key) . '=' . $value;
			}
		}
		$setStr = implode(',', $set);
		return ' SET ' . $setStr;
	}

	/**
	 
	 * value分析
	 
	 * @access protected
	 
	 * @param mixed $value
	 
	 * @return string
	
	 */
	protected function parseValue($value) {
		if (is_int($value)) {
			$value = intval($value);
		}
		elseif (is_float($value)) {
			$value = floatval($value);
		}
		elseif (is_string($value)) {
			$value = '\'' . $this->escape_string($value) . '\'';
		}
		elseif (isset ($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp') {
			$value = $this->escape_string($value[1]);
		}
		elseif (is_null($value)) {
			$value = 'null';
		}
		return $value;
	}

	/**
	
	 * field分析
	
	 * @access protected
	
	 * @param mixed $fields
	
	 * @return string
	
	 */
	protected function parseField($fields) {
		if (is_array($fields)) {
			// 完善数组方式传字段名的支持
			// 支持 'field1'=>'field2' 这样的字段别名定义
			$array = array ();
			foreach ($fields as $key => $field) {
				if (!is_numeric($key)) {
					$array[] = $this->addSpecialChar($key) . ' AS ' . $this->addSpecialChar($field);
				} else {
					$array[] = $this->addSpecialChar($field);
				}
			}
			$fieldsStr = implode(',', $array);
		}
		elseif (is_string($fields) && !empty ($fields)) {
			$fieldsStr = $this->addSpecialChar($fields);
		} else {
			$fieldsStr = '*';
		}
		return $fieldsStr;
	}

	/**
	
	 * table分析
	
	 * @access protected
	
	 * @param mixed $table
	
	 * @return string
	
	 */
	protected function parseTable($tables) {
		$parseStr = '';
		if (is_string($tables)) {
			$tables = explode(',', $tables);
			if(!empty($this->dbPrefix)){
			foreach($tables as $value ){
				$table[]=$this->dbPrefix."_".$value;
				}
			} else {
				$table=$tables;
				}
		}
		array_walk($table, array (
			& $this,
			'addSpecialChar'
		));
		$parseStr = implode(',', $table);
		return $parseStr;
	}

	/**
	
	 * where分析
	
	 * @access protected
	
	 * @param mixed $where
	
	 * @return string
	
	 */
	protected function parseWhere($where) {
		$whereStr = '';
		if (is_string($where) && strpos($where, '&'))
			parse_str($where, $where);
		if (is_string($where)) {
			// 直接使用字符串条件
			$whereStr = $where;
		} else { // 使用数组条件表达式
			if (array_key_exists('_logic', $where)) {
				// 定义逻辑运算规则 例如 OR XOR AND NOT
				$operate = ' ' . strtoupper($where['_logic']) . ' ';
				unset ($where['_logic']);
			} else {
				// 默认进行 AND 运算
				$operate = ' AND ';
			}
			foreach ($where as $key => $val) {
				$key = $this->addSpecialChar($key);
				$whereStr .= "( ";
				if (is_array($val)) {
					if (is_string($val[0])) {
						if (preg_match('/^(EQ|NEQ|GT|EGT|LT|ELT|NOTLIKE|LIKE)$/i', $val[0])) { // 比较运算
							$whereStr .= $key . ' ' . $this->comparison[strtolower($val[0])] . ' ' . $this->parseValue($val[1]);
						}
						elseif ('exp' == strtolower($val[0])) { // 使用表达式
							$whereStr .= ' (' . $key . ' ' . $val[1] . ') ';
						}
						elseif (preg_match('/IN/i', $val[0])) { // IN 运算
							$zone = is_array($val[1]) ? implode(',', $this->parseValue($val[1])) : $val[1];
							$whereStr .= $key . ' ' . strtoupper($val[0]) . ' (' . $zone . ')';
						}
						elseif (preg_match('/BETWEEN/i', $val[0])) { // BETWEEN运算
							$data = is_string($val[1]) ? explode(',', $val[1]) : $val[1];
							$whereStr .= ' (' . $key . ' BETWEEN ' . $data[0] . ' AND ' . $data[1] . ' )';
						} else {
							throw_exception(L('_EXPRESS_ERROR_') . ':' . $val[0]);
						}
					} else {
						$count = count($val);
						if (in_array(strtoupper(trim($val[$count -1])), array (
								'AND',
								'OR',
								'XOR'
							))) {
							$rule = strtoupper(trim($val[$count -1]));
							$count = $count -1;
						} else {
							$rule = 'AND';
						}
						for ($i = 0; $i < $count; $i++) {
							$data = is_array($val[$i]) ? $val[$i][1] : $val[$i];
							if ('exp' == strtolower($val[$i][0])) {
								$whereStr .= '(' . $key . ' ' . $data . ') ' . $rule . ' ';
							} else {
								$op = is_array($val[$i]) ? $this->comparison[strtolower($val[$i][0])] : '=';
								$whereStr .= '(' . $key . ' ' . $op . ' ' . $this->parseValue($data) . ') ' . $rule . ' ';
							}
						}
						$whereStr = substr($whereStr, 0, -4);
					}
				} else {
					//对字符串类型字段采用模糊匹配
					if (C('LIKE_MATCH_FIELDS') && preg_match('/(' . C('LIKE_MATCH_FIELDS') . ')/i', $key)) {
						$val = '%' . $val . '%';
						$whereStr .= $key . " LIKE " . $this->parseValue($val);
					} else {
						$whereStr .= $key . " = " . $this->parseValue($val);
					}
				}
				$whereStr .= ' )' . $operate;
			}
			$whereStr = substr($whereStr, 0, -strlen($operate));
		}
		return empty ($whereStr) ? '' : ' WHERE ' . $whereStr;
	}

	/**
	
	 * limit分析
	
	 * @access protected
	
	 * @param mixed $lmit
	
	 * @return string
	
	 */
	protected function parseLimit($limit) {
		return !empty ($limit) ? ' LIMIT ' . $limit . ' ' : '';
	}

	/**
	
	 * join分析
	
	 * @access protected
	
	 * @param mixed $join
	
	 * @return string
	
	 */
	protected function parseJoin($join) {
		$joinStr = '';
		if (!empty ($join)) {
			if (is_array($join)) {
				foreach ($join as $key => $_join) {
					if (false !== stripos($_join, 'JOIN')) {
						$joinStr .= ' ' . $_join;
					} else {
						$joinStr .= ' LEFT JOIN ' . $_join;
					}
				}
			} else {
				$joinStr .= ' LEFT JOIN ' . $join;
			}
		}
		return $joinStr;
	}

	/**
	
	 * order分析
	
	 * @access protected
	
	 * @param mixed $order
	
	 * @return string
	
	 */
	protected function parseOrder($order) {
		return !empty ($order) ? ' ORDER BY ' . $order : '';
	}

	/**
	
	 * group分析
	
	 * @access protected
	
	 * @param mixed $group
	
	 * @return string
	
	 */
	protected function parseGroup($group) {
		return !empty ($group) ? ' GROUP BY ' . $group : '';
	}

	/**
	
	 * having分析
	
	 * @access protected
	
	 * @param string $having
	
	 * @return string
	
	 */
	protected function parseHaving($having) {
		return !empty ($having) ? ' HAVING ' . $having : '';
	}

	/**
	
	 * distinct分析
	
	 * @access protected
	
	 * @param mixed $distinct
	
	 * @return string
	
	 */
	protected function parseDistinct($distinct) {
		return !empty ($distinct) ? ' DISTINCT ' : '';
	}

	/**
	
	 * 插入记录
	
	 * @access public
	
	 * @param mixed $data 数据
	 * @param array $options 参数表达式
	
	 * @return false | integer
	
	 */
	public function insert($data, $options = array ()) {
		foreach ($data as $key => $val) {
			$value = $this->parseValue($val);
			if (is_scalar($value)) { // 过滤非标量数据
				$values[] = $value;
				$fields[] = $this->addSpecialChar($key);
			}
		}
		$sql = 'INSERT INTO ' . $this->parseTable($options['table']) . ' (' . implode(',', $fields) . ') VALUES (' . implode(',', $values) . ')';
		//$sql = 'INSERT INTO ' . $this->parseTable($options['table']) . $this->parseSet($data); 两种写法
		$this->queryString= $sql;
		return $this->query($sql);
	}
	
	

	public    function 	batchinsert($data, $options = array ()) {
$sql = 'INSERT INTO ' . $this->parseTable($options['table']) .'(trace_accuser,trace_cur_loc,trace_ref_url,trace_data,trace_userip,trace_useragent,trace_timestamp)      values'.$data;
		//$sql = 'INSERT INTO ' . $this->parseTable($options['table']) . $this->parseSet($data); 两种写法
		$this->queryString= $sql;
		return $this->query($sql);
	}
	
	
	

	/**
	
	 * 更新记录
	
	 * @access public
	
	 * @param mixed $data 数据
	 * @param array $options 表达式
	
	 * @return false | integer
	
	 */
	public function update($data, $options) {
		$sql = 'UPDATE ' .
		$this->parseTable($options['table']) .
		$this->parseSet($data) .
		$this->parseWhere(isset ($options['where']) ? $options['where'] : '') .
		$this->parseOrder(isset ($options['order']) ? $options['order'] : '') .
		$this->parseLimit(isset ($options['limit']) ? $options['limit'] : '');
		$this->queryString= $sql;
		//return $this->execute($sql);
		return $this->query($sql);
	}

	/**
	
	 * 删除记录
	
	 * @access public
	
	 * @param array $options 表达式
	
	 * @return false | integer
	
	 */
	public function delete($options = array ()) {
		$sql = 'DELETE FROM ' .
		$this->parseTable($options['table']) .
		$this->parseWhere(isset ($options['where']) ? $options['where'] : '') .
		$this->parseOrder(isset ($options['order']) ? $options['order'] : '') .
		$this->parseLimit(isset ($options['limit']) ? $options['limit'] : '');
		//return $this->execute($sql);
		$this->queryString= $sql;
	
		return $this->query($sql);
	}

	/**
	
	 * 查找记录
	
	 * @access public
	
	 * @param array $options 表达式
	
	 * @return array
	
	 */
	public function select($options = array ()) {
		$sql = str_replace(array (
			'%TABLE%',
			'%DISTINCT%',
			'%FIELDS%',
			'%JOIN%',
			'%WHERE%',
			'%GROUP%',
			'%HAVING%',
			'%ORDER%',
			'%LIMIT%'
		), array (
			$this->parseTable($options['table']),
			$this->parseDistinct(isset ($options['distinct']) ? $options['distinct'] : false),
			$this->parseField(isset ($options['field']) ? $options['field'] : '*'),
			$this->parseJoin(isset ($options['join']) ? $options['join'] : ''),
			$this->parseWhere(isset ($options['where']) ? $options['where'] : ''),
			$this->parseGroup(isset ($options['group']) ? $options['group'] : ''),
			$this->parseHaving(isset ($options['having']) ? $options['having'] : ''),
			$this->parseOrder(isset ($options['order']) ? $options['order'] : ''),
			$this->parseLimit(isset ($options['limit']) ? $options['limit'] : '')
		), $this->selectSql);
		
		
		//echo $sql."<br>";
		
		$this->queryString= $this->CheckSql($sql,'select');
		return $this->query($this->CheckSql($sql,'select'));
	}
	/**
	
	 * 字段和表名添加`
	 * 保证指令中使用关键字不出错 针对mysql
	
	 * @access protected
	
	 * @param mixed $value
	
	 * @return mixed
	
	 */
	protected function addSpecialChar(& $value) {
		if (0 === strpos($this->dbType, 'MYSQL')) {
			$value = trim($value);
			if (false !== strpos($value, ' ') || false !== strpos($value, ',') || false !== strpos($value, '*') || false !== strpos($value, '(') || false !== strpos($value, '.') || false !== strpos($value, '`')) {
				//如果包含* 或者 使用了sql方法 则不作处理
			} else {
				$value = '`' . $value . '`';
			}
		}
		return $value;
	}

	/**
	 +----------------------------------------------------------
	 * 数据库操作方法
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param string $sql  执行语句
	 +----------------------------------------------------------
	 * @return void
	 +----------------------------------------------------------
	 */
	public function execute($sql) {
		return $this->_execute($sql);
	}
	
	
    /**
     +----------------------------------------------------------
     * 执行语句 针对 INSERT, UPDATE 以及DELETE
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @param string $str  sql指令
     +----------------------------------------------------------
     * @return integer
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _execute($str='') {
        if ( !$this->linkID ) return false;
        if ( $str != '' ) $this->queryStr = $str;
        //释放前次的查询结果
        $result =   mysql_query($this->queryStr, $this->linkID) ;
        if ( false === $result) {
                mysql_errormsg($this->error());
        } else {
            $this->numRows = mysql_affected_rows($this->linkID);
            $this->lastInsID = mysql_insert_id($this->linkID);
            return $this->numRows;
        }
    }
	
	
	
	
	
	
     /**
     +----------------------------------------------------------
     * SQL指令安全过滤
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $str  SQL字符串
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    
    
    public function escape_string($str) {
        return mysql_escape_string($str);
    }
    
	/**
		 * 输出sql语句错误信息
		 *
		 * @access public
		 * @parameter string $msg	提示信息
		 * @parameter string $sql	错误sql语句
		 * @return void
		 */
	public function mysql_errormsg($msg = '', $sql = '') {
		$sql=$this->queryString;
		$message = "<html>\n<head>\n";
		$message .= "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\">\n";
		$message .= "<STYLE TYPE=\"text/css\">\n";
		$message .= "body,td,p,pre {\n";
		$message .= "font-family : Verdana, sans-serif;font-size : 12px;\n";
		$message .= "}\n";
		$message .= "</STYLE>\n";
		$message .= "</head>\n";
		$message .= "<body>\n";
		$message .= "<div style=\"border:#666 1px solid; line-height:18px; background-color:#FEF8E0; text-align:left;width:500px;padding:15px;\">\n";

		$content = "<b>错误提示:<br/>" . htmlspecialchars($msg) . "</b><br/>\n";
		$content .= "<b>Mysql error description</b>: <font color=red>" . $this->error() . "</font>\n<br>";
		$content .= "<b>Mysql error number</b>: " . $this->errno() . "\n<br>";
		$content .= "<b>Date</b>: " . date("Y-m-d  H:i:s") . "\n<br>";
		$content .= "<b>Script</b>: http://" . $_SERVER[HTTP_HOST] . getenv("REQUEST_URI") . "\n<br>";
		$content .= "<b>Referer</b>: " . $sql . "\n";

		$message .= $content;
		$message .= "</div></body>\n</html>";
		echo $message;
		exit;
	}

	//获得当前的脚本网址
	function GetCurUrl() {
		if (!empty ($_SERVER["REQUEST_URI"])) {
			$scriptName = $_SERVER["REQUEST_URI"];
			$nowurl = $scriptName;
		} else {
			$scriptName = $_SERVER["PHP_SELF"];
			if (empty ($_SERVER["QUERY_STRING"])) {
				$nowurl = $scriptName;
			} else {
				$nowurl = $scriptName . "?" . $_SERVER["QUERY_STRING"];
			}
		}
		return $nowurl;
	}



/**
 * 获得客户端真实的IP地址
 *
 * @access public
 * @return void
 */
function GetIp() {
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
		$ip = getenv("HTTP_CLIENT_IP");
	} else
		if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		} else
			if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
				$ip = getenv("REMOTE_ADDR");
			} else
				if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
					$ip = $_SERVER['REMOTE_ADDR'];
				} else {
					$ip = "unknown";
				}
	return ($ip);
}
//防止注入
public function CheckSql($db_string, $querytype = 'select') {
	global $cfg_cookie_encode;
	$clean = '';
	$error = '';
	$old_pos = 0;
	$pos = -1;
	$log_file = 'safe.txt';
	$userIP = $this->GetIp();
	$getUrl = $this->GetCurUrl();
	$times=date("Y-m-d @ H:i");

	//如果是普通查询语句，直接过滤一些特殊语法
	if ($querytype == 'select') {
		$notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";

		//$notallow2 = "--|/\*";
		if (eregi($notallow1, $db_string)) {
			fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||$times||SelectBreak\r\n");
			exit ("<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>");
		}
	}

	//完整的SQL检查
	while (true) {
		$pos = strpos($db_string, '\'', $pos +1);
		if ($pos === false) {
			break;
		}
		$clean .= substr($db_string, $old_pos, $pos - $old_pos);
		while (true) {
			$pos1 = strpos($db_string, '\'', $pos +1);
			$pos2 = strpos($db_string, '\\', $pos +1);
			if ($pos1 === false) {
				break;
			}
			elseif ($pos2 == false || $pos2 > $pos1) {
				$pos = $pos1;
				break;
			}
			$pos = $pos2 +1;
		}
		$clean .= '$s$';
		$old_pos = $pos +1;
	}
	$clean .= substr($db_string, $old_pos);
	$clean = trim(strtolower(preg_replace(array (
		'~\s+~s'
	), array (
		' '
	), $clean)));

	//老版本的Mysql并不支持union，常用的程序里也不使用union，但是一些黑客使用它，所以检查它
	if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) {
		$fail = true;
		$error = "union detect";
	}

	//发布版本的程序可能比较少包括--,#这样的注释，但是黑客经常使用它们
	elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, '#') !== false) {
		$fail = true;
		$error = "comment detect";
	}

	//这些函数不会被使用，但是黑客会用它来操作文件，down掉数据库
	elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0) {
		$fail = true;
		$error = "slown down detect";
	}
	elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
		$fail = true;
		$error = "slown down detect";
	}
	elseif (strpos($clean, 'load_file') !== false && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0) {
		$fail = true;
		$error = "file fun detect";
	}
	elseif (strpos($clean, 'into outfile') !== false && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0) {
		$fail = true;
		$error = "file fun detect";
	}

	//老版本的MYSQL不支持子查询，我们的程序里可能也用得少，但是黑客可以使用它来查询数据库敏感信息
	/*elseif (preg_match('~\([^)]*?select~s', $clean) != 0) {
		$fail = true;
		$error = "sub select detect";
	}*/
	if (!empty ($fail)) {
		fputs(fopen($log_file, 'a+'), "$times||$userIP||$getUrl||$db_string||$error\r\n");
		exit ("<font size='5' color='red'>Safe Alert: Request Error step 2!</font>");
	} else {
		return $db_string;
	}

}
}
?>